package com.huatai.vba.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

	public static String generalDate(String str, String oldFmStr, String newFmStr) throws ParseException {
		Date date = new SimpleDateFormat(oldFmStr).parse(str);
		return new SimpleDateFormat(oldFmStr).format(date);
	}

	public static String replaceDateStr(String str, Date date) {
		String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
		String yyyyMMdd = dateStr.replace("-", "");
		String yyMMdd = yyyyMMdd.substring(2, 8);
		String yyMM = yyyyMMdd.substring(2, 6);
		String yyyy = yyyyMMdd.substring(0, 4);
		String yy = yyyyMMdd.substring(2, 4);
		String MMdd = yyyyMMdd.substring(4, 8);
		String MM = yyyyMMdd.substring(4, 6);
		String dd = yyyyMMdd.substring(6, 8);
		String y_M_d = new SimpleDateFormat("y-M-d").format(date);
		str = str.replace("|yyyy-MM-dd|", dateStr);
		str = str.replace("|yyyyMMdd|", yyyyMMdd);
		str = str.replace("|yyMMdd|", yyMMdd);
		str = str.replace("|yyyy|", yyyy);
		str = str.replace("|yyMM|", yyMM);
		str = str.replace("|yy|", yy);
		str = str.replace("|MMdd|", MMdd);
		str = str.replace("|MM|", MM);
		str = str.replace("|dd|", dd);
		str = str.replace("|y-M-d|", y_M_d);
		str = str.replace("|M#|", convertExaMonth(MM));
		System.out.println(str);
		return str;
	}

	public static String convertExaMonth(String MM) {
		if ("01".equals(MM))
			return "1";
		if ("02".equals(MM))
			return "2";
		if ("03".equals(MM))
			return "3";
		if ("04".equals(MM))
			return "4";
		if ("05".equals(MM))
			return "5";
		if ("06".equals(MM))
			return "6";
		if ("07".equals(MM))
			return "7";
		if ("08".equals(MM))
			return "8";
		if ("09".equals(MM))
			return "9";
		if ("10".equals(MM))
			return "a";
		if ("11".equals(MM))
			return "b";
		if ("12".equals(MM))
			return "c";
		return null;
	}
}
